home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / network / atre12.zip / LF.ZIP / LFEDIT.C < prev    next >
C/C++ Source or Header  |  1991-07-05  |  13KB  |  390 lines

  1. #include <windows.h>
  2. #include "lfedit.h"
  3. #define EDITID 1
  4.  
  5. long FAR PASCAL WndProc(HWND, WORD, WORD, LONG);
  6. extern BOOL NEAR PASCAL main(HWND, LPSTR, LPSTR); /*in lf.c*/
  7.  
  8. BOOL ReadFile (HANDLE, HWND, HWND, POFSTRUCT, char *, BOOL) ;
  9. BOOL WriteFile (HANDLE, HWND, HWND, POFSTRUCT, char *, BOOL) ;
  10. BOOL PrintFile (HANDLE, HWND, HWND, char *);
  11.  
  12. LPSTR lstrrchr (LPSTR, char);
  13.  
  14. char szAppName []  = "lfEdit";
  15. char szFileSpec [] = "*.LF";
  16. static char szUntitled [] = "(untitled)";
  17.  
  18. char szFileNameIn[96] ;
  19. char szFileNameOut[96];
  20.  
  21. BOOL bIsProcessing = FALSE;
  22.  
  23. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  24.                    LPSTR lpszCmdLine, int nCmdShow)
  25.  
  26.     {
  27.     HWND hwnd;
  28.     MSG msg;
  29.     HANDLE hAccel;
  30.     WNDCLASS wndclass;
  31.  
  32.     if (!hPrevInstance)
  33.         {
  34.         wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  35.         wndclass.lpfnWndProc    = WndProc;
  36.         wndclass.cbClsExtra     = 0;
  37.         wndclass.cbWndExtra     = 0;
  38.         wndclass.hInstance      = hInstance;
  39.         wndclass.hIcon          = LoadIcon(hInstance, "lfico");
  40.         wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  41.         wndclass.hbrBackground  = GetStockObject(WHITE_BRUSH);
  42.         wndclass.lpszMenuName   = szAppName;
  43.         wndclass.lpszClassName  = szAppName;
  44.  
  45.         RegisterClass(&wndclass);
  46.         }
  47.  
  48.     else
  49.         {
  50.         MessageBox(NULL, "LF is already running!", "LF", MB_OK);
  51.         return FALSE;
  52.         }
  53.  
  54.  
  55.     hwnd = CreateWindow(szAppName, NULL,
  56.                         WS_OVERLAPPEDWINDOW,
  57.                         GetSystemMetrics(SM_CXSCREEN) / 4,
  58.                         GetSystemMetrics(SM_CYSCREEN) / 4,
  59.                         GetSystemMetrics(SM_CXSCREEN) / 2,
  60.                         GetSystemMetrics(SM_CYSCREEN) / 2,
  61.                         NULL, NULL, hInstance, lpszCmdLine) ;
  62.  
  63.     ShowWindow (hwnd, nCmdShow);
  64.  
  65.     UpdateWindow (hwnd);
  66.  
  67.     hAccel = LoadAccelerators(hInstance, szAppName);
  68.  
  69.     while (GetMessage (&msg, NULL, 0, 0))
  70.         {
  71.         if (!TranslateAccelerator(hwnd, hAccel, &msg))
  72.             {
  73.             TranslateMessage(&msg);
  74.             DispatchMessage(&msg);
  75.             }
  76.         }
  77.     return msg.wParam;
  78.     }
  79.  
  80. #pragma argsused
  81.  
  82. BOOL FAR PASCAL AboutDlgProc(HWND hDlg, WORD message,
  83.                              WORD wParam, LONG lParam)
  84.     {
  85.     switch(message)
  86.         {
  87.         case WM_INITDIALOG:
  88.             return TRUE;
  89.  
  90.         case WM_COMMAND :
  91.             switch (wParam)
  92.                 {
  93.                 case IDOK:
  94.                     EndDialog(hDlg, 0);
  95.                     return TRUE;
  96.                 }
  97.             break;
  98.         }
  99.     return FALSE;
  100.     }
  101.  
  102. void DoCaption (HWND hwnd, char *szFileName)
  103.     {
  104.     char szCaption [40];
  105.  
  106.     wsprintf(szCaption, "%s - %s", (LPSTR) szAppName,
  107.               (LPSTR) (szFileName[0] ? szFileName : szUntitled));
  108.  
  109.     SetWindowText(hwnd, szCaption);
  110.     }
  111.  
  112. short AskAboutSave (HWND hwnd, char *szFileName)
  113.     {
  114.     char szBuffer[40];
  115.     short nReturn;
  116.  
  117.     wsprintf(szBuffer, "Save current changes: %s",
  118.              (LPSTR) (szFileName[0] ? szFileName : szUntitled));
  119.  
  120.     if (IDYES == (nReturn = MessageBox(hwnd, szBuffer, szAppName,
  121.                                 MB_YESNOCANCEL | MB_ICONQUESTION)))
  122.  
  123.         if (!SendMessage(hwnd, WM_COMMAND, IDM_SAVE, 0L))
  124.             return IDCANCEL;
  125.  
  126.     return nReturn;
  127.     }
  128.  
  129. short AskAboutProcess (HWND hwnd, char *szFileName)
  130.     {
  131.     char szBuffer[80];
  132.     short nReturn;
  133.  
  134.     wsprintf(szBuffer, "Quitting while processing %s \nmay produce disastrous results!",
  135.                 (LPSTR) szFileName);
  136.  
  137.     nReturn = MessageBox(hwnd, szBuffer, szAppName,
  138.                                 MB_OKCANCEL | MB_ICONQUESTION);
  139.  
  140.     return nReturn;
  141.     }
  142.  
  143. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  144.     {
  145.     static BOOL     bNeedSave = FALSE;
  146.     static char     szRealFileName[16];
  147.     static FARPROC  lpfnAboutDlgProc;
  148.     static HANDLE   hInst;
  149.     static HWND     hwndEdit;
  150.     char            szFileName[16];
  151.     LONG            lSelect;
  152.     OFSTRUCT        of;
  153.     WORD            wEnable;
  154.  
  155.     switch(message)
  156.         {
  157.         case WM_CREATE :
  158.             hInst = ((LPCREATESTRUCT) lParam) -> hInstance;
  159.             lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc, hInst);
  160.  
  161.             hwndEdit = CreateWindow ("edit", NULL,
  162.                     WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
  163.                         WS_BORDER | ES_LEFT | ES_MULTILINE |
  164.                         ES_AUTOHSCROLL | ES_AUTOVSCROLL,
  165.                     0,0,0,0,
  166.                     hwnd, EDITID, hInst, NULL);
  167.  
  168.             SendMessage (hwndEdit, EM_LIMITTEXT, 64000, 0L);
  169.  
  170.             if (lstrlen (((LPCREATESTRUCT) lParam) -> lpCreateParams))
  171.                 {
  172.                 OpenFile (((LPCREATESTRUCT) lParam) -> lpCreateParams, &of,
  173.                                 OF_PARSE);
  174.  
  175.                 lstrcpy  (szFileName, AnsiNext (lstrrchr (of.szPathName, '\\')));
  176.  
  177.                 if (ReadFile (hInst, hwnd, hwndEdit, &of, szFileName, FALSE))
  178.                     lstrcpy(szRealFileName, szFileName);
  179.                 }
  180.  
  181.             DoCaption (hwnd, szRealFileName);
  182.             return 0;
  183.  
  184.         case WM_SETFOCUS:
  185.             SetFocus(hwndEdit);
  186.             return 0;
  187.  
  188.         case WM_SIZE:
  189.             MoveWindow (hwndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  190.             return 0;
  191.  
  192.         case WM_INITMENUPOPUP:
  193.             if (lParam == 1)        /* Edit Menu */
  194.                 {
  195.                 EnableMenuItem(wParam, IDM_UNDO,
  196.                     SendMessage(hwndEdit, EM_CANUNDO, 0, 0L) ?
  197.                         MF_ENABLED : MF_GRAYED);
  198.  
  199.                 EnableMenuItem(wParam, IDM_PASTE,
  200.                     IsClipboardFormatAvailable(CF_TEXT) ?
  201.                         MF_ENABLED : MF_GRAYED);
  202.  
  203.                 lSelect = SendMessage(hwndEdit, EM_GETSEL, 0, 0L);
  204.  
  205.                 if (HIWORD(lSelect) == LOWORD(lSelect))
  206.                     wEnable = MF_GRAYED;
  207.                 else
  208.                     wEnable = MF_ENABLED;
  209.  
  210.                 EnableMenuItem (wParam, IDM_CUT,   wEnable);
  211.                 EnableMenuItem (wParam, IDM_COPY,  wEnable);
  212.                 EnableMenuItem (wParam, IDM_CLEAR, wEnable);
  213.                 }
  214.  
  215.             else if (lParam == 0)   /*File menu */
  216.                 {
  217.                 if (szRealFileName[0])
  218.                     wEnable = MF_ENABLED;
  219.                 else
  220.                     wEnable = MF_GRAYED;
  221.  
  222.                 EnableMenuItem (wParam, IDM_RUN,   wEnable);
  223.                 EnableMenuItem (wParam, IDM_PRINT, wEnable);
  224.                 }
  225.  
  226.             return 0;
  227.  
  228.         case WM_COMMAND:
  229.             if (LOWORD(lParam) && wParam == EDITID)
  230.                 {
  231.                 switch (HIWORD(lParam))
  232.                     {
  233.                     case EN_UPDATE :
  234.                         bNeedSave = TRUE;
  235.                         return 0;
  236.  
  237.                     case EN_ERRSPACE :
  238.                         MessageBox(hwnd, "Editor is out of space.",
  239.                             szAppName, MB_OK | MB_ICONSTOP);
  240.  
  241.                         return 0;
  242.                     }
  243.                 break;
  244.                 }
  245.  
  246.             switch (wParam)
  247.                 {
  248.                 case IDM_NEW :
  249.                     if(bNeedSave && IDCANCEL == AskAboutSave(hwnd, szRealFileName))
  250.                         return 0;
  251.  
  252.                     SetWindowText(hwndEdit, "\0");
  253.                     szRealFileName[0] = '\0';
  254.                     DoCaption (hwnd, szRealFileName);
  255.                     bNeedSave = FALSE;
  256.                     return(0);
  257.  
  258.                 case IDM_OPEN :
  259.                     if (bNeedSave && IDCANCEL ==
  260.                             AskAboutSave (hwnd, szRealFileName))
  261.                         return 0;
  262.  
  263.                     if (ReadFile (hInst, hwnd, hwndEdit, &of, szFileName, TRUE))
  264.                         {
  265.                         lstrcpy (szRealFileName, szFileName);
  266.                         DoCaption (hwnd, szRealFileName);
  267.                         bNeedSave = FALSE;
  268.                         }
  269.  
  270.                     return 0;
  271.  
  272.                 case IDM_SAVE :
  273.                     if (szRealFileName[0])
  274.                         {
  275.                         if (WriteFile(hInst, hwnd, hwndEdit, &of, szRealFileName, FALSE))
  276.                             {
  277.                             bNeedSave = FALSE;
  278.                             return 1;
  279.                             }
  280.                         return 0;
  281.                         }
  282.                                                     /* fall through */
  283.  
  284.                 case IDM_SAVEAS:
  285.                     if (WriteFile (hInst, hwnd, hwndEdit, &of, szFileName, TRUE))
  286.                         {
  287.                         lstrcpy (szRealFileName, szFileName);
  288.                         DoCaption (hwnd, szFileName);
  289.                         bNeedSave = FALSE;
  290.                         return 1;
  291.                         }
  292.                     return 0;
  293.  
  294.                 case IDM_PRINT:
  295.                     PrintFile(hInst, hwnd, hwndEdit,
  296.                         szRealFileName[0] ? (LPSTR) szRealFileName : szUntitled);
  297.  
  298.                     return 0;
  299.  
  300.  
  301.                 case IDM_RUN:
  302.                     if (bIsProcessing)
  303.                         {
  304.                         char szBuffer[80];
  305.                         wsprintf(szBuffer, "Already processing %s ", (LPSTR) szFileNameIn);
  306.                         (void) MessageBox(hwnd, szBuffer, szAppName,
  307.                                     MB_OK | MB_ICONEXCLAMATION);
  308.                         }
  309.  
  310.                     else
  311.                         {
  312.                         if (!bNeedSave || IDCANCEL !=
  313.                                    AskAboutSave (hwnd, szRealFileName))
  314.                             {
  315.                             lstrcpy(szFileNameIn, szRealFileName[0] ? szRealFileName : "lf.in");
  316.                             if (GetOutFile(hInst, hwnd))
  317.                                 {
  318.                                 bIsProcessing = TRUE;
  319.                                 if (!main(hwnd, szFileNameIn, szFileNameOut))
  320.                                     {
  321.                                     char szBuffer[80];
  322.                                     wsprintf(szBuffer,"Could not execute %s ",
  323.                                                    (LPSTR) szFileNameIn);
  324.                                     MessageBeep(0);
  325.                                     MessageBox(hwnd,szBuffer, "LF Error",
  326.                                          MB_OK | MB_ICONEXCLAMATION);
  327.                                     }
  328.                                 }
  329.                                 bIsProcessing = FALSE;
  330.                             }
  331.                         }
  332.                     return 0;
  333.  
  334.  
  335.                 case IDM_EXIT:
  336.                     SendMessage(hwnd, WM_CLOSE, 0, 0L);
  337.                     return 0;
  338.  
  339.                 case IDM_ABOUT:
  340.                     DialogBox(hInst, "AboutBox", hwnd, lpfnAboutDlgProc);
  341.                     return 0;
  342.  
  343.                 case IDM_UNDO:
  344.                     SendMessage(hwndEdit, WM_UNDO, 0, 0L);
  345.                     return 0;
  346.  
  347.                 case IDM_CUT:
  348.                     SendMessage(hwndEdit, WM_CUT, 0, 0L);
  349.                     return 0;
  350.  
  351.                 case IDM_COPY:
  352.                     SendMessage(hwndEdit, WM_COPY, 0, 0L);
  353.                     return 0;
  354.  
  355.                 case IDM_PASTE:
  356.                     SendMessage(hwndEdit, WM_PASTE, 0, 0L);
  357.                     return 0;
  358.  
  359.                 case IDM_CLEAR:
  360.                     SendMessage(hwndEdit, WM_CLEAR, 0, 0L);
  361.                     return 0;
  362.  
  363.                 case IDM_SELALL:
  364.                     SendMessage(hwndEdit, EM_SETSEL, 0, MAKELONG(0, 32767));
  365.                     return 0;
  366.                 }
  367.             break;
  368.  
  369.         case WM_CLOSE :
  370.             if (!bNeedSave || IDCANCEL != AskAboutSave (hwnd, szRealFileName))
  371.                 if (!bIsProcessing || IDCANCEL != AskAboutProcess(hwnd, szFileNameIn))
  372.                     DestroyWindow(hwnd);
  373.  
  374.             return 0;
  375.  
  376.         case WM_QUERYENDSESSION :
  377.             if (!bNeedSave || IDCANCEL != AskAboutSave (hwnd, szRealFileName))
  378.                 if (!bIsProcessing || IDCANCEL != AskAboutProcess(hwnd, szFileNameIn))
  379.                     return 1L;
  380.  
  381.             return 0;
  382.  
  383.         case WM_DESTROY:
  384.             PostQuitMessage(0);
  385.             return  0;
  386.  
  387.         }
  388.     return DefWindowProc(hwnd, message, wParam, lParam);
  389.     }
  390.